package com.example.controller;
import com.example.entity.Student;
import com.example.service.StudentService;
import com.example.utils.PageResult;
import com.example.utils.R;
import com.example.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
//@CrossOrigin // 解决跨域问题
public class
StudentController {

    @Autowired
    private StudentService studentService;
    //1. 获取学生列表
    @GetMapping("/list")
    public List<Student> list(){
        return studentService.findAll();
    }

//    2. 分页查询学生信息
    @GetMapping("/findByPage")
    public PageResult<Student> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        return studentService.findByPage(page,pageSize);
    }

    // 3. 条件查询带分页
//     @RequestBody : 将前端传入的json对象转换为java对象
//     @ResponseBody : 将java对象转换为json对象
    @PostMapping("/search")
    public PageResult<Student> search(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestBody(required = false) StudentVO studentVo){
        return studentService.search(page,pageSize,studentVo);
    }

    // 4. 保存学生
    @PostMapping("/save")
    public R save(@RequestBody Student student){
        System.out.println(student);
        return studentService.save(student) ? R.ok().message("添加成功！") : R.error().message("添加失败！");
    }
}